package com.gitee.shardingsphere.controller;

import com.gitee.shardingsphere.common.entity.User;
import com.gitee.shardingsphere.service.IUserService;
import io.shardingsphere.core.constant.SQLType;
import io.shardingsphere.core.routing.router.masterslave.MasterSlaveRouter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 开发人员   YooLin1c
 * 编写日期   2018/6/21
 * 联系邮箱   401664157@qq.com
 * 作用描述   读写分离测试接口
 *
 * @see MasterSlaveRouter#route(SQLType)
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 查询用户列表
     * 走从库
     *
     * @return
     */
    @GetMapping
    public List<User> getUser() {
        return userService.list(null);
    }

    /**
     * 创建/修改用户信息
     * 走主库
     *
     * @param user
     * @return
     */
    @PostMapping
    public User saveUser(@RequestBody User user) {
        return userService.save(user) ? userService.getById(user.getId()) : null;
    }
}
